//Process: follow the numbers, starting at main.

package cards;

import java.util.List;

import javax.swing.JOptionPane;

public class WarGame extends CardGame {

  @Override
  protected int getMaxPlayers() {
    return 0;
  }
  
  @Override
  protected int getMinPlayers() {
    return 0;
  }
  
    //3. Run 'initPlayers' method, then 'deal', then 'score'
  public void play(int numPlayers) {
    initPlayers(numPlayers);
    deal(new HoyleDeck(), numPlayers);
    score();
  }
  
  //4. Creates players for the number of players inputed.
  private void initPlayers(int numPlayers) {
    for (int i = 0; i < numPlayers; i++) {
    	int preferedCount = i + 1;
      this.players.add(new Player("Player " + (preferedCount)));
    }
  }
 
  private void deal(HoyleDeck deck, int numPlayers) {
		int numDealt = 0;
		int extraCards = 0;
		extraCards = 52 % numPlayers;
		Card card = deck.deal(numPlayers);
		//5. deck.deal will not deal the extra cards because they are below the deal level  (>)
		//6. Gives a card to each player
		for (int i = 52; i > extraCards; i--) {
			this.players.get(numDealt++ % numPlayers).getHand().add(card);
			card = deck.deal(numPlayers);
		}
	}
  
  private void score() {
    for (Player p : this.players) {
    	//7. Gets the hand of each player and sends it to 'countHand' method
      int playerScore = countHand(p.getHand());
      //9. Prints players and their score.
      //10. END OF TRANSMISSION
      System.out.println(p.getName() + ": " + playerScore);
      p.setScore(playerScore);
    }
  }

  private int countHand(List<Card> hand) {
	  //8. Method takes the ordial value of all the values in 'Card.java' and adds them and returns them to 'score' method
    int sum = 0;
    for (Card card : hand) {
      sum += card.getFaceValue().ordinal() + 1;
    }
    return sum;
  }


  
  public static void main(String[] args) {
	  
	  //1. Get numPlaers
	  String input = JOptionPane.showInputDialog
		("How many players are there?");
		int numPlayers = Integer.parseInt(input);
		
		//2. Run "play" method (go up to 3)
    new WarGame().play(numPlayers);
    
  }

}

